<template>
  <div>
    <Cate-Head />
    <div class="wrap2">
      <div class="cate-left">
        <div class="cates">
          <div
            v-for="(cate, index) in cates"
            :key="cate.id"
            @click="switchTab(index)"
            :class="[
              'cate',
              {
                active: index === num,
              },
            ]"
          >
            {{ cate.name }}
          </div>
        </div>
      </div>
      <div>
        <img src="" alt="" />
      </div>
      <div class="cate-right">
        <div class="items">
          <div
            class="item"
            v-for="(item, index) in items"
            :key="item.id"
            @click="enterDetail(item.id)"
          >
            <img v-lazy="item.pic" class="img" alt="" />
            <div class="item-info">
              <h5 class="name">{{ item.name }}</h5>
              <p class="desc">
                {{ item.characteristic ? item.characteristic : "暂无描述" }}
              </p>
              <div class="acion">
                <p class="price">￥{{ item.minPrice }}</p>
                <van-icon @click.stop="add(item.id, index)" name="cart-o" />
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <common-tab />
  </div>
</template>

<script>
import CateHead from './components/CateHead.vue'
import CommonTab from 'components/CommonTab'
import { fetchCates, fetchItems, addCart } from 'api'
import { Toast } from 'vant'
import { isLogin, getToken } from 'utils'
export default {
  name: 'cate',
  data () {
    return {
      cates: [],
      items: [],
      num: 0
    }
  },
  created () {
    this.fetCates()
  },
  methods: {
    enterDetail (id) {
      this.$router.push({
        path: '/detail',
        query: {
          id
        }
      })
    },
    add (id, index) {
      const path = this.$route.path
      // 加入购物车动作 需要登录才能访问
      if (!isLogin()) {
        Toast.fail({
          message: '请登录',
          duration: 1000,
          onClose: () => {
            this.$router.push({
              path: '/login',
              query: {
                from: path
              }
            })
          }
        })
        return false
      }
      // 登陆了 加入购物车
      addCart({
        goodsId: id,
        number: 1,
        token: getToken()
      }).then((res) => {
        if (res.data.code === 0) {
          Toast.success('加入成功，请前往购物车结算')
          this.$store.commit('cart/set_items', res.data.data.items)
        }
      })
    },
    switchTab (index) {
      // 点击切换分类
      this.num = index
      this.fetItems()
    },
    fetCates () {
      fetchCates().then((res) => {
        if (res.data.code === 0) {
          this.cates = res.data.data
          this.fetItems()
        }
      })
    },
    fetItems () {
      this.items = []
      fetchItems({
        categoryId: this.cates[this.num].id
      }).then((res) => {
        if (res.data.code === 0) {
          this.items = res.data.data.result
        }
      })
    }
  },
  components: {
    CommonTab,
    CateHead
  }
}
</script>

<style lang="scss" scoped>
.wrap2 {
  position: fixed;
  padding-top: 50px;
  left: 0;
  right: 0;
  top: 0;
  bottom: 50px;
  display: flex;
  .cate-left {
    width: 110px;
    border-right: 1px solid #ccc;
    height: 100%;
    background-color: #f1f1f1;
    overflow-y: auto;
    .cate {
      line-height: 46px;
      font-size: 14px;
      text-align: center;
      color: #333;
      &.active {
        border-left: 2px solid #ea063c;
        background-color: #ffff;
      }
    }
  }
  .cate-right {
    flex: 1;
    height: 100%;
    overflow-y: auto;
    .items {
      .item {
        height: 230px;
        margin: 8px 0;
        display: flex;
        background-color: #fff;
        border-radius: 8px;
        overflow: hidden;
        align-items: center;
        .img {
          width: 160px;
          height: 160px;
        }
        .item-info {
          flex: 1;
          height: 100%;
          padding-top: 20px;
          padding-left: 5px;
          .name {
            font-size: 14px;
            color: #333;
            margin-top: 10px;
            line-height: 20px;
          }
          .desc {
            width: 100%;
            font-size: 12px;
            color: #666;
            margin-top: 20px;
            line-height: 20px;
          }
          .acion {
            display: flex;
            padding-right: 10px;
            height: 50px;
            align-items: center;
            justify-content: space-between;
            font-size: 16px;
            color: #ff734c;
          }
        }
      }
    }
  }
}
</style>
